﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Forms;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using AMS.Common;
using AMS.ComponentModel;

namespace AMS.Trademark.Management
{
    /// <summary>
    /// 编辑、添加收文对话框
    /// </summary>
    public partial class EditRecvFileDlg : BaseForm
    {
        public EditRecvFileDlg()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        public string RFIndex { get; private set; }
        private BusinessAction action;
        private RFCategoryInfo recvFile;

        /// <summary>
        /// 编辑收文
        /// </summary>
        /// <param name="rfIndex"></param>
        /// <returns></returns>
        public DialogResult Edit(string rfIndex)
        {
            RFIndex = rfIndex;
            recvFile = rfDetail.DataBind(rfIndex) as RFCategoryInfo;
            if (recvFile == null)
            {
                MessageBox.Show("收文不存在，请刷新后重试");
                this.DialogResult = DialogResult.Cancel;
                Close();
                return this.DialogResult;
            }

            Text = string.Format("编辑收文 - {0}", rfIndex);
            action = BusinessAction.Edit;
            rfDetail.SetEditStatus();
            return ShowDialog();
        }

        /// <summary>
        /// 新建收文
        /// </summary>
        /// <returns></returns>
        public DialogResult NewRecvFile()
        {
            action = BusinessAction.New;
            Text = "添加收文";
            rfDetail.SetNewStatus();
            return ShowDialog();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (action == BusinessAction.View)
            {
                MessageBox.Show("请使用 Edit 或 NewRecvFile 接口，而不要直接使用ShowDialog");
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
                return;
            }

            if (!rfDetail.CheckData())
            {
                return;
            }

            IRFTaskManager rftask = ObjectUtility.GetObject<IRFTaskManager>("RFTask");
            if (action == BusinessAction.New)
            {
                recvFile = rftask.GetRecvFile(rfDetail.txtRFIndex.Text);
                if (recvFile != null)
                {
                    MessageBox.Show("收文编号已经存在，请重新输入");
                    return;
                }

                // 添加收文
                recvFile = new RFCategoryInfo();
                rfDetail.UpdateRecvFile(recvFile);
                rftask.AddRecvFile(recvFile);
                MessageBox.Show("添加收文信息成功");
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
            {
                rfDetail.UpdateRecvFile(recvFile);
                if (rftask.UpdateRecvFile(recvFile))
                {
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show("更新收文信息失败，请刷新后再编辑");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
